﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        public void YGRGBDAILY(DataSet db)
        {
            int x = 4, y = 1;
            string VKPPOSTYP = "";
            DataTable dtOutput = new DataTable();
            try
            {

                #region======Title===========
                int id = 1;
               
                exAgent.SetCellValue(3, id++, "LO"); dtOutput.Columns.Add("LO", typeof(System.String));
                exAgent.SetCellValue(3, id++, "LB"); dtOutput.Columns.Add("LB", typeof(System.String));
                exAgent.SetCellValue(3, id++, "包装"); dtOutput.Columns.Add("BEM1", typeof(System.String));
                exAgent.SetCellValue(3, id++, "纱型"); dtOutput.Columns.Add("FBPART", typeof(System.String));
                exAgent.SetCellValue(3, id++, "纺批"); dtOutput.Columns.Add("SPPART", typeof(System.String));
                exAgent.SetCellValue(3, id++, "捻批"); dtOutput.Columns.Add("ZWPART", typeof(System.String));
                exAgent.SetCellValue(3, id++, "纱线"); dtOutput.Columns.Add("ART", typeof(System.String));
                exAgent.SetCellValue(3, id++, "变量"); dtOutput.Columns.Add("VAR", typeof(System.String));
                exAgent.SetCellValue(3, id++, "色号"); dtOutput.Columns.Add("FARBE", typeof(System.String));
                exAgent.SetCellValue(3, id++, "包装组合"); dtOutput.Columns.Add("PATTEN", typeof(System.String));
                exAgent.SetCellValue(3, id++, "当前库存"); dtOutput.Columns.Add("STOCKQTY", typeof(System.Double));
                exAgent.SetCellValue(3, id++, "昨日库存"); dtOutput.Columns.Add("BS00", typeof(System.Double));
                exAgent.SetCellValue(3, id++, "采购"); dtOutput.Columns.Add("BS11", typeof(System.Double));
                exAgent.SetCellValue(3, id++, "生产"); dtOutput.Columns.Add("BS13", typeof(System.Double));
                exAgent.SetCellValue(3, id++, "发货"); dtOutput.Columns.Add("BS20", typeof(System.Double));
                exAgent.SetCellValue(3, id++, "接收"); dtOutput.Columns.Add("BS21", typeof(System.Double));
                exAgent.SetCellValue(3, id++, "手工出库"); dtOutput.Columns.Add("BS42", typeof(System.Double));
                exAgent.SetCellValue(3, id++, "手工入库"); dtOutput.Columns.Add("BS43", typeof(System.Double));
                #endregion

                var qGW = from GW in db.Tables["ICC_STOCK"].AsEnumerable()
                          group GW by new
                          {
                              PT = GW["MDATE"].ToString(),
                          }
                              into g
                              select new
                              {
                                  KEY = g.Key,
                              };
                foreach (var GW in qGW)
                {
                    string MDATE = GW.KEY.PT;
                    exAgent.SetCellValue(1, 3, "成品仓库" + Helpers.Helper.Left(MDATE, 4) + "年" + Helpers.Helper.Right(MDATE, 2) + ")月报" + Environment.NewLine + date);
                    exAgent.RenameSheet(date);
                    DataRow[] rows = db.Tables["ICC_STOCK"].Select();
                    DataTable dtSoruce = rows.CopyToDataTable();
                    dtOutput.Clear();
                    dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);
                    foreach (DataRow rx in dtOutput.Rows)
                    {
                        rx["BEM1"] = GetGB2312Value(rx["BEM1"].ToString());
                        rx["FBPART"] = GetGB2312Value(rx["FBPART"].ToString());
                        rx["SPPART"] = GetGB2312Value(rx["SPPART"].ToString());
                        rx["ZWPART"] = GetGB2312Value(rx["ZWPART"].ToString());
                    }
                    #region====Excel==========
                    int count = dtOutput.Rows.Count;
                    exAgent.RangeCopy(4, 50, 5, 4 + count);
                    exAgent.DataTableToExcel(dtOutput, x, 1);
                    //MergeSame(x, count);
                    exAgent.SetCellValue(1, 1, 1);
                    exAgent.BatchConvert2Number(4, 11, count + 3, 18);
                    exAgent.SetCellValue(1, 1, " ");
                    exAgent.SetCellValue(1, 3, MDATE+"每日库存");
                    #endregion
                    break;
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("完成", StatusEventType.StopProgress);
            }
        }

    }
}